﻿Public Class frmExamens

    Private szSQL As String
    Private rs As ADODB.Recordset

    Private Sub frmExamens_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' initialiser le combo Type examens
        CB_TypeExamen.SelectedIndex = 2
        TE_datedeb.Value = getLundiPrecedent(Now)

    End Sub

    Private Sub CB_TypeExamen_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CB_TypeExamen.SelectedIndexChanged
        ' charger la liste sélectionnée
        InitListeExamens(0)
    End Sub

    Private Sub InitListeExamens(ByVal ix As Integer)

        Dim rsEleve As ADODB.Recordset = Nothing
        Dim nb As Integer = 0
        Dim Statut As String = "V"
        Dim szDate As String

        szDate = Format(TE_datedeb.Value, "yyyy/MM/dd")
        ' effacer la listview
        LV_EXAMENS.Items.Clear()

        ' Lire la table EXAMENS
        szSQL = "CALL spGet_EXAMENS_byPERMIS('" & CB_TypeExamen.SelectedItem.ToString & "','" & szDate & "')"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            If rs.Fields("valide").Value > 0 Then
                Statut = "V"
            Else
                Statut = ""
            End If
            LV_EXAMENS.Items.Add(rs.Fields("date").Value)
            LV_EXAMENS.Items(nb).SubItems.Add(Format(rs.Fields("heure").Value, "HH:mm"))
            LV_EXAMENS.Items(nb).SubItems.Add(rs.Fields("societe").Value)
            LV_EXAMENS.Items(nb).SubItems.Add(rs.Fields("agence").Value)
            LV_EXAMENS.Items(nb).SubItems.Add(rs.Fields("no_exa").Value)
            LV_EXAMENS.Items(nb).SubItems.Add(rs.Fields("cle").Value)
            LV_EXAMENS.Items(nb).SubItems.Add(rs.Fields("lieu").Value)
            LV_EXAMENS.Items(nb).SubItems.Add(rs.Fields("total").Value)
            LV_EXAMENS.Items(nb).SubItems.Add(rs.Fields("total").Value - rs.Fields("inscrits").Value)  ' en fait, c'est le dispo
            LV_EXAMENS.Items(nb).SubItems.Add(Statut)
            If Statut = "V" Then
                LV_EXAMENS.Items(nb).ForeColor = Color.Green
            End If
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_EXAMENS)

    End Sub

    Private Sub TE_datedeb_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TE_datedeb.ValueChanged
        InitListeExamens(0)
    End Sub

    Private Sub LV_Examens_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_EXAMENS.DoubleClick

        If LV_EXAMENS.SelectedItems.Count > 0 Then
            ' examen appartient à l'agence de l'utilisateur ?
            If gUserAgence <> LV_EXAMENS.SelectedItems(0).SubItems(3).Text And gUserAcces < 6 Then
                Exit Sub
            End If

            frmExamensMAJ.pType_exam = CB_TypeExamen.SelectedItem.ToString
            frmExamensMAJ.pDate = LV_EXAMENS.SelectedItems(0).Text
            frmExamensMAJ.pNo_exa = LV_EXAMENS.SelectedItems(0).SubItems(4).Text
            frmExamensMAJ.pAgence = LV_EXAMENS.SelectedItems(0).SubItems(3).Text
            frmExamensMAJ.ShowDialog()

            ' réaffiche la viewlist
            InitListeExamens(LV_EXAMENS.SelectedItems(0).Index)
        End If

    End Sub

    Private Sub LV_Examens_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_EXAMENS.KeyDown
        Select Case e.KeyCode
            ' touche Enter = DoubleClick
            Case Keys.Enter
                LV_Examens_DoubleClick(sender, e)

            Case Keys.Insert
                BO_ADD_LIGNE_Click(sender, e)

        End Select

    End Sub

    Private Sub BO_ADD_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        ' Niveau d'autorisation ?
        If IsUserAuthorised(6) = False Then
            Exit Sub
        End If
        FrmExamenADD.pType_exam = CB_TypeExamen.SelectedItem.ToString
        FrmExamenADD.ShowDialog()

        ' réaffiche la viewlist
        InitListeExamens(0)
    End Sub
End Class